package com.hishop.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.Query;
import com.hishop.member.dao.MemberDao;
import com.hishop.member.dao.MemberLevelDao;
import com.hishop.member.entity.MemberEntity;
import com.hishop.member.entity.MemberLevelEntity;
import com.hishop.member.exception.PhoneExistException;
import com.hishop.member.exception.UserNameExistException;
import com.hishop.member.service.MemberService;
import com.hishop.member.vo.MemberLoginVo;
import com.hishop.member.vo.MenberRegistVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Resource
    MemberLevelDao memberLevelDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");

        QueryWrapper<MemberEntity> memberEntityQueryWrapper =new  QueryWrapper<MemberEntity>();

        if (!StringUtils.isEmpty(key)) {
            memberEntityQueryWrapper.eq("id",key)
                    .or().like("level_id",key)
                    .or().like("username",key)
                    .or().like("nickname",key);
        }
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                memberEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();

        MemberDao memberDao = this.getBaseMapper();
        MemberEntity entity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username",loginacct).or().eq("mobile",loginacct));
        if (entity == null){
            return null;
        }else {
            String passwordDb = entity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //密码匹配
            boolean matches = passwordEncoder.matches(password,passwordDb);
            if (matches){
                return entity;
            }else {
                return null;
            }
        }
    }

    @Override
    public void regist(MenberRegistVo vo) {
        MemberDao memberDao = this.getBaseMapper();
        MemberEntity entity = new MemberEntity();

        //设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());

        ckeckPhoneUnique(vo.getPhone());

        ckeckUserNameUnique(vo.getUserName());

        //设置
        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());

        entity.setNickname(vo.getUserName());



        // MD5加密
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);

        memberDao.insert(entity);
    }

    @Override
    public void ckeckPhoneUnique(String phone) throws PhoneExistException {
        MemberDao memberDao = this.getBaseMapper();
        Long phoneCount = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile",phone) );
        if (phoneCount > 0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void ckeckUserNameUnique(String name) throws UserNameExistException {
        MemberDao memberDao = this.getBaseMapper();
        Long usernameCount = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username",name) );
        if (usernameCount > 0) {
            throw new UserNameExistException();
        }
    }


}